<?php
// 数据库配置
$db_host = '127.0.0.1';
$db_user = 'kuaidi';
$db_pass = 'kSmLPzAzejm3frnf';
$db_name = 'kuaidi';
$db_port = 3306;

$conn = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
if ($conn->connect_error) {
    die('数据库连接失败: ' . $conn->connect_error);
}
$conn->set_charset('utf8');

// 获取所有表名
$tables = [];
$res = $conn->query("SHOW TABLES");
while ($row = $res->fetch_array()) {
    $tables[] = $row[0];
}

// 选择的表
$selected_table = isset($_GET['table']) ? $_GET['table'] : (count($tables) ? $tables[0] : '');

// 获取表内容
$rows = [];
$fields = [];
if ($selected_table) {
    $result = $conn->query("SELECT * FROM `" . $conn->real_escape_string($selected_table) . "` LIMIT 100");
    if ($result) {
        $fields = $result->fetch_fields();
        while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
    }
}

// 处理添加 name 列并填充
if (isset($_POST['add_name']) && $selected_table) {
    // 添加 name 列（如果不存在）
    $conn->query("ALTER TABLE `" . $conn->real_escape_string($selected_table) . "` ADD COLUMN name VARCHAR(64) NOT NULL DEFAULT ''");
    // 填充所有行
    $conn->query("UPDATE `" . $conn->real_escape_string($selected_table) . "` SET name = 'tanbaolong'");
    // 刷新页面
    header("Location: db_browser.php?table=" . urlencode($selected_table));
    exit();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>数据库浏览器</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background: #f5f7fa; }
        .main-card { background: #fff; border-radius: 12px; box-shadow: 0 2px 12px rgba(0,0,0,0.06); padding: 2rem; margin: 2rem auto; max-width: 1100px; }
        .table-responsive { margin-top: 1.5rem; }
    </style>
</head>
<body>
<div class="main-card">
    <h2>数据库浏览器</h2>
    <form method="get" class="mb-3" style="display:inline-block;">
        <label for="table" class="form-label">选择数据表：</label>
        <select name="table" id="table" class="form-select" style="max-width:300px;display:inline-block;" onchange="this.form.submit()">
            <?php foreach($tables as $table): ?>
                <option value="<?php echo htmlspecialchars($table); ?>" <?php if($table==$selected_table) echo 'selected'; ?>><?php echo htmlspecialchars($table); ?></option>
            <?php endforeach; ?>
        </select>
    </form>
    <?php if($selected_table): ?>
        <form method="post" style="display:inline-block;margin-left:1rem;">
            <input type="hidden" name="table" value="<?php echo htmlspecialchars($selected_table); ?>">
            <button type="submit" name="add_name" class="btn btn-success btn-sm" onclick="return confirm('确定要为表 <?php echo htmlspecialchars($selected_table); ?> 添加 name 列并填充？');">添加 name 列并填充</button>
        </form>
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-sm align-middle">
                <thead class="table-light">
                    <tr>
                        <?php foreach($fields as $field): ?>
                            <th><?php echo htmlspecialchars($field->name); ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($rows as $row): ?>
                        <tr>
                            <?php foreach($fields as $field): ?>
                                <td><?php echo htmlspecialchars($row[$field->name]); ?></td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php if(count($rows) == 100): ?>
                <div class="text-muted">仅显示前100条记录</div>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>
</body>
</html> 